<template>
  <a-layout class="admin-layout">
    <!-- 侧边栏 -->
    <a-layout-sider 
      :trigger="null" 
      v-model:collapsed="collapsed" 
      collapsible
      class="admin-sider"
      :width="220"
    >
      <div class="logo-container">
        <div class="logo" />
        <h1 class="logo-text" v-if="!collapsed">商品管理系统</h1>
      </div>
      <a-menu 
        v-model:selectedKeys="selectedKeys" 
        theme="dark" 
        mode="inline"
        class="admin-menu"
      >
        <a-menu-item key="/admin/index" @click="router.push('/admin/index')">
          <template #icon><home-outlined /></template>
          <span>首页</span>
        </a-menu-item>
        <a-menu-item key="/admin/manage" @click="router.push('/admin/manage')">
          <template #icon><AppstoreOutlined /></template>
          <span>管理页面</span>
        </a-menu-item>
        <a-menu-item key="/admin/users" @click="router.push('/admin/users')">
          <template #icon><UserOutlined /></template>
          <span>用户管理</span>
        </a-menu-item>
      </a-menu>
    </a-layout-sider>
    <!-- 右侧内容 -->
    <a-layout class="site-layout">
      <!-- 头部 -->
      <a-layout-header ref="headerRef" class="admin-header">
        <div class="header-left">
          <div class="trigger-container">
            <menu-unfold-outlined
              v-if="collapsed"
              class="trigger"
              @click="() => (collapsed = !collapsed)"
            />
            <menu-fold-outlined v-else class="trigger" @click="() => (collapsed = !collapsed)" />
          </div>
        </div>
        <AdminHeader />
      </a-layout-header>
      <!-- 内容 -->
      <a-layout-content class="content-container">
        <div class="content-area" :style="{ maxHeight: `${contentHeight}px` }">
          <router-view />
        </div>
      </a-layout-content>
      <!-- 底部 -->
      <a-layout-footer ref="footerRef" class="admin-footer"> 通用开发模板 by wsy @2025 </a-layout-footer>
    </a-layout>
  </a-layout>
</template>
<script setup>
import { computed, ref, onMounted, onUnmounted, nextTick } from 'vue'
import { 
  PieChartOutlined, 
  MenuFoldOutlined, 
  MenuUnfoldOutlined,
  HomeOutlined,
  AppstoreOutlined,
  SettingOutlined,
  UserOutlined
} from '@ant-design/icons-vue'
import { useRouter } from 'vue-router'
import AdminHeader from '@/components/admin/AdminHeader.vue'
import request from '@/api/request'

//变量定义
const collapsed = ref(false)
const windowHeight = ref(window.innerHeight)
const router = useRouter()
const headerHeight = ref(64) // 默认顶部高度
const footerHeight = ref(64) // 默认底部高度

// DOM引用
const headerRef = ref(null)
const footerRef = ref(null)

// 布局常量
const MARGIN_RESERVE = 10   // 额外的边距保留

//方法定义
const handleResize = () => {
  windowHeight.value = window.innerHeight
  updateComponentHeights()
}

const updateComponentHeights = () => {
  // 设置延迟确保DOM已更新
  nextTick(() => {
    // 强制重新计算contentHeight
    headerHeight.value = headerRef.value?.offsetHeight || 64
    footerHeight.value = footerRef.value?.offsetHeight || 64
  })
}

//计算属性
const contentHeight = computed(() => {
  // 窗口高度 - 头部高度 - 底部高度 - 布局边距 - 内容边距 - 额外保留
  return windowHeight.value - headerHeight.value - footerHeight.value - MARGIN_RESERVE
})

const selectedKeys = computed(() => {
  return router.currentRoute.value.path ? [router.currentRoute.value.path] : ['/admin/index']
})

//生命周期钩子
onMounted(() => {
  window.addEventListener('resize', handleResize)
  updateComponentHeights() // 初始化组件高度
})

onUnmounted(() => {
  window.removeEventListener('resize', handleResize)
})
</script>
<style lang="scss" scoped>
.admin-layout {
  min-height: 100vh;
}

.admin-sider {
  box-shadow: 2px 0 6px rgba(0, 21, 41, 0.25);
  z-index: 10;
  
  :deep(.ant-layout-sider-children) {
    display: flex;
    flex-direction: column;
  }
}

.logo-container {
  height: 64px;
  padding: 0 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  transition: all 0.3s;
}

.logo {
  width: 32px;
  height: 32px;
  background: rgba(255, 255, 255, 0.8);
  border-radius: 6px;
  margin-right: 8px;
  flex-shrink: 0;
}

.logo-text {
  color: white;
  font-size: 18px;
  font-weight: 600;
  white-space: nowrap;
  margin: 0;
}

.admin-menu {
  flex: 1;
  border-right: 0;
  
  :deep(.ant-menu-item), :deep(.ant-menu-submenu-title) {
    margin: 0;
    border-radius: 0;
    
    &::after {
      display: none;
    }
  }
  
  :deep(.ant-menu-item-selected) {
    background-color: #1890ff !important;
    
    .anticon {
      color: white;
    }
  }
}

.site-layout {
  background: #f0f2f5;
  transition: all 0.3s;
}

.admin-header {
  background: white;
  padding: 0;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
  display: flex;
  justify-content: space-between;
  align-items: center;
  z-index: 9;
  position: relative;
  height: 64px;
}

.header-left {
  display: flex;
  align-items: center;
}

.trigger-container {
  padding: 0 24px;
}

.trigger {
  font-size: 18px;
  line-height: 64px;
  cursor: pointer;
  transition: color 0.3s;
  
  &:hover {
    color: #1890ff;
  }
}

.content-container {
  margin: 16px;
  overflow-y: auto;
  scrollbar-width: none; /* Firefox */
  -ms-overflow-style: none; /* IE and Edge */
  transition: all 0.3s;
  
  &::-webkit-scrollbar {
    display: none; /* Chrome, Safari and Opera */
  }
}

.content-area {
  padding: 24px;
  background: #fff;
  min-height: 420px;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  overflow-y: auto;
  scrollbar-width: none; /* Firefox */
  -ms-overflow-style: none; /* IE and Edge */
  
  &::-webkit-scrollbar {
    display: none; /* Chrome, Safari and Opera */
  }
}

.admin-footer {
  text-align: center;
  padding: 12px 24px;
  font-size: 14px;
  color: rgba(0, 0, 0, 0.65);
  transition: all 0.3s;
}

@media (max-width: 992px) {
  .content-container {
    margin: 12px;
  }
  
  .content-area {
    padding: 16px;
  }
}
</style>
